#!/usr/bin/env python

"""
Functions: see README
VERBOSE
"""

import hashlib
import json
import math
import random
import string
import sys
import time

try: # Use psyco if available
	import psyco
	psyco.full()
except ImportError:
	pass

LENFORCOIILIDE = input('number of hexis to collide (full length 32 for MD5)')
print LENFORCOIILIDE, "full image:", 16**LENFORCOIILIDE,  "expected security:", 4**LENFORCOIILIDE
Number_of_rounds_to_run = input('number of  - birthdays - (full length (recommend: for MD5)')

 
VERBOSE = 10
SPLITER = '-'

add_hex2 = lambda hex1, hex2:  (str(hex(int(hex1, 16) + int(hex2, 16))))[-len(hex1):]

"""when last symbol is expected to be L"""
add_long_hex2 = lambda hex1, hex2:  (str(hex(int(hex1, 16) + int(hex2, 16))))[-LENFORCOIILIDE-1:-1]

#print add_hex2('ff', 'ff')


start = time.clock()


def Random_Oracle(message):
	return hashlib.md5(message).hexdigest()

def Add_Random_Oracle(message1, message2):
	return hashlib.md5(message1).hexdigest()
#	return hashlib.sha256(message).hexdigest()


def querying_random_oracle(number):
	randomness = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(10)])
	value = Random_Oracle("1alo 2vet 3 ute")
	print value,randomness
	return value,randomness

	


def main():

	#Ta = eval(open("hash_database_new").read())

	#Ta = ravlyuchenko_s_table(Ta)
	kalnova_s_table({})

	#print>>file("hash_database", 'w'), Ta


def ravlyuchenko_s_table(T):

#	print T

	for x in xrange(0,10000000):
		randomness = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(15)])
#		randomness = "aaa"
		if Random_Oracle(randomness) in T:
			print "collide!!!"
			T[Random_Oracle(randomness)].append(randomness)
		else:
			T[Random_Oracle(randomness)] = [randomness]
		if x%1000000 == 0:
			print x, Random_Oracle(randomness), T[Random_Oracle(randomness)]

#	print T
	return T

def kalnova_s_table(T):

#	print T
	collides = 0
	for x in xrange(0,Number_of_rounds_to_run):
		randomness1 = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(10)])
		randomness2 = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(10)])
		
#		randomness = '8lNt8myRrT'

		tar = add_long_hex2(Random_Oracle(randomness1), Random_Oracle(randomness2))

		if tar in T:
			T[tar].append([randomness1, randomness2])
			collides += 1
			print collides, "omg!!!!!!!!! collides!!!", tar, T[tar]

			
		else:
			T[tar]=[[randomness1, randomness2]]
		if x%100000 == 0:
			print collides, x, Random_Oracle(randomness1), Random_Oracle(randomness2), tar, T[tar]




main()


print time.clock() - start
